import { routerRedux } from 'dva/router';
import * as userService from '../services/system/UserService';

export default {
  namespace: 'login',

  state: {
    status: 200,
    submitting:false,
  },

  effects: {
    *login({ payload }, { call, put }) {
      yield put({
        type: 'changeSubmitting',
        payload: true,
      });
      const response = yield call(userService.login, payload);
      yield put({
        type: 'changeLoginStatus',
        payload: response,
      });
      // Login successfully
      if (response.code === 200) {
        yield put(routerRedux.push('/'));
      }
    },
    *logout(_, { call, put }) {
      const response = yield call(userService.logout);
        if (response.code === 200) {
            yield put(routerRedux.push('/user/login'));
        }
    },
  },

  reducers: {
    changeLoginStatus(state, { payload }) {
      return {
        ...state,
        status: payload.code,
        submitting: false,
      };
    },
    changeSubmitting(state, { payload }) {
      return {
        ...state,
        submitting: payload,
      };
    },
  },
};
